<template>
  <div class="app-container">

    <el-row>
      <el-col :span="16">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['scxx:bom:add']"
        >新增</el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['scxx:bom:edit']"
        >修改</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['scxx:bom:remove']"
        >删除</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['scxx:bom:export']"
        >导出</el-button>
      </el-col>

      <el-col :span="8">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
                 style="display: flex;justify-content: center">
          <el-form-item prop="isUse">
            <el-select v-model="queryParams.isUse" placeholder="请选择是否启用" clearable>
              <el-option
                v-for="dict in dict.type.is_use"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="bomName">
            <el-input
              v-model="queryParams.bomName"
              placeholder="请输入BOM名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item class="yu" style="display: flex">
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="BOM编码" align="center" prop="bomCode" />
      <el-table-column label="BOM名称" align="center" prop="bomName" />
      <el-table-column label="产品名称" align="center" prop="cpName" />
      <el-table-column label="BOM分类" align="center" prop="bomType" />
      <el-table-column label="物料版本" align="center" prop="wlVersion" />
      <el-table-column label="是否启用" align="center" prop="isUse">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.is_use" :value="scope.row.isUse"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['scxx:bom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['scxx:bom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产BOM对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">

        <el-row>
          <el-col :span="12">
            <el-form-item label="BOM名称" prop="bomName">
              <el-input v-model="form.bomName" placeholder="请输入BOM名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="BOM分类" prop="bomSortcode">
              <el-input v-model="form.bomSortcode" placeholder="请输入" readonly
                        @click.native="dialogVisible=true;getCpflList()" suffix-icon="el-icon-more"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料版本" prop="wlVersion">
              <el-input v-model="form.wlVersion" placeholder="请输入物料版本" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select v-model="form.isUse" placeholder="请选择是否启用">
                <el-option
                  v-for="dict in dict.type.is_use"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="产品编码" prop="cpCode">
          <el-input v-model="form.cpCode" placeholder="请输入" readonly
                    @click.native="dialogVisibleCp=true;getCpList()" suffix-icon="el-icon-more"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    产品分类-->
    <el-dialog
      title="选择产品分类"
      :visible.sync="dialogVisible"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectCpflWithName"
                        v-model="selectCpfl.cpflName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectCpflWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectCpfl.cpflName='';getCpflList();" type="info">重置</el-button>
            </el-col>
          </el-row>

        </el-header>
        <el-main>

          <!-- 展示产品分类信息 -->
          <el-table
            :data="cpData"
            height="250"
            border
            style="width: 100%"
            @row-click="getCpData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="cpflCode"
              label="产品分类编码"
              align="center"
            >
            </el-table-column>
            <el-table-column
              prop="cpflName"
              label="产品分类名单"
              align="center"
            >
            </el-table-column>
          </el-table>
          <pagination
            v-show="cpflTotal>0"
            :total="cpflTotal"
            :page.sync="selectCpfl.pageNum"
            :limit.sync="selectCpfl.pageSize"
            @pagination="getCpflList"
          />
        </el-main>
      </el-container>
    </el-dialog>

    <!--    产品-->
    <el-dialog
      title="选择产品"
      :visible.sync="dialogVisibleCp"
      width="30%">
      <el-container>
        <el-header>
          <el-row>
            <el-col :span="14">
              <el-input suffix-icon="el-icon-search" @keyup.enter.native="selectCpWithName"
                        v-model="selectCp.cpName"></el-input>
            </el-col>
            <el-col :span="10">
              <el-button plain style="margin-left: 10px" @click="selectCpWithName()" type="primary">搜索</el-button>
              <el-button plain style="margin-left: 5px" @click="selectCp.cpName='';getCpList();" type="info">重置</el-button>
            </el-col>
          </el-row>

        </el-header>
        <el-main>

          <!-- 展示产品信息 -->
          <el-table
            :data="Data"
            height="250"
            border
            style="width: 100%"
            @row-click="getData"
            :header-cell-style="{'text-align':'center'}"
          >
            <el-table-column
              prop="cpName"
              label="产品名称"
              align="center">
            </el-table-column>
            <el-table-column
              prop="cpTypeName"
              label="产品类型"
              align="center">
            </el-table-column>
            <el-table-column
              prop="cpType"
              label="产品型号"
              align="center">
            </el-table-column>
          </el-table>
          <pagination
            v-show="cpTotal>0"
            :total="cpTotal"
            :page.sync="selectCp.pageNum"
            :limit.sync="selectCp.pageSize"
            @pagination="getCpList"
          />
        </el-main>
      </el-container>
    </el-dialog>
  </div>
</template>

<script>
import { listBom, getBom, delBom, addBom, updateBom } from "@/api/scxx/bom";
import { getCpfl } from "@/api/scxx/qxdy";
import { getCp } from "@/api/scxx/standard";

export default {
  name: "Bom",
  dicts: ['is_use'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产BOM表格数据
      bomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bomName: null,
        cpCode: null,
        bomSortcode: null,
        wlVersion: null,
        isUse: null,
        isDelete: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        bomName: [
          { required: true, message: "BOM名称不能为空", trigger: "blur" }
        ],
        cpCode: [
          { required: true, message: "产品编码不能为空", trigger: "blur" }
        ],
        bomSortcode: [
          { required: true, message: "BOM分类不能为空", trigger: "blur" }
        ],
        wlVersion: [
          { required: true, message: "物料版本不能为空", trigger: "blur" }
        ],
        isUse: [
          { required: true, message: "是否启用不能为空", trigger: "change" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ]
      },
      // 选择产品分类的弹出层
      dialogVisible: false,
      // 产品分类信息
      cpData:[],
      // 产品分类搜索值
      selectCpfl: {
        pageNum: 1,
        pageSize: 10,
        cpflName: "",
      },
      // 产品总条数
      cpflTotal:0,
      // 选择产品的弹出层
      dialogVisibleCp: false,
      // 产品信息
      Data:[],
      // 产品搜索值
      selectCp: {
        pageNum: 1,
        pageSize: 10,
        cpName: "",
      },
      // 产品总条数
      cpTotal:0
    };
  },
  created() {
    this.getList()
  },
  methods: {
    // 根据产品类型名模糊查询
    selectCpWithName(){
      if (this.selectCp.cpName.trim() != "" ){
        this.getCpList();
      }
    },
    // 获取产品分类选择的数据
    getData(row, column, event){
      this.form.cpCode = row.cpCode
      this.dialogVisibleCp = false
    },
    // 根据条件查询产品
    getCpList(){
      getCp(this.selectCp).then(response => {
        this.Data = response.rows;
        this.cpTotal = response.total;
      });
    },

    // 根据产品类型名模糊查询
    selectCpflWithName(){
      if (this.selectCpfl.cpflName.trim() != "" ){
        this.getCpflList();
      }
    },

    // 获取产品分类选择的数据
    getCpData(row, column, event){
      this.form.bomSortcode = row.cpflCode
      this.dialogVisible = false
    },

    // 根据条件查询产品分类
    getCpflList(){
      getCpfl(this.selectCpfl).then(response => {
        this.cpData = response.rows;
        this.cpflTotal = response.total;
      });
    },
    /** 查询生产BOM列表 */
    getList() {
      this.loading = true;
      listBom(this.queryParams).then(response => {
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bomCode: null,
        bomName: null,
        cpCode: null,
        bomSortcode: null,
        wlVersion: null,
        isUse: null,
        isDelete: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bomCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加生产BOM";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bomCode = row.bomCode || this.ids
      getBom(bomCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改生产BOM";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bomCode != null) {
            updateBom(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBom(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bomCodes = row.bomCode || this.ids;
      this.$modal.confirm('是否确认删除生产BOM编号为"' + bomCodes + '"的数据项？').then(function() {
        return delBom(bomCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('scxx/bom/export', {
        ...this.queryParams
      }, `bom_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scope lang="scss">
.yu {
  .el-form-item__content {
    display: flex;
  }
}
</style>
